//
//  DemandNoteViewController.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/7/1.
//

import UIKit
import SnapKit
import JXSegmentedView

enum DemandStatus: String {
  /// 待缴费
  case unpaidFee = "待缴费"
  /// 已缴费
  case paidFee = "已缴费"
}

/// 缴费单
class DemandNoteViewController: UIViewController {

  let titles: [DemandStatus] = [.unpaidFee, .paidFee]

  let segmentedView = JXSegmentedView()
  let segmentedDataSource = JXSegmentedTitleDataSource()

  lazy var listContainerView = JXSegmentedListContainerView(dataSource: self)

  let sheetHandler = PTSheetHandle()

    override func viewDidLoad() {
        super.viewDidLoad()

      opacityNavigationBar(color: .designKit.background)

      view.backgroundColor = .designKit.background

      let feesButtonItem = UIBarButtonItem(title: "收费标准", style: .plain, target: self, action: #selector(tappedFees))
      feesButtonItem.tintColor = .designKit.accent
      feesButtonItem.setTitleTextAttributes([.font: UIFont.designKit.body], for: .normal)
      navigationItem.rightBarButtonItem = feesButtonItem

      let searchView = R.nib.searchContentView(owner: self)!
      view.addSubview(searchView)

      segmentedView.delegate = self
      view.addSubview(segmentedView)

      segmentedDataSource.titles = titles.map { $0.rawValue }
      segmentedDataSource.isTitleColorGradientEnabled = true
      segmentedView.dataSource = self.segmentedDataSource
      segmentedView.backgroundColor = .designKit.background
      segmentedDataSource.titleSelectedColor = .designKit.accent

      let indicator = JXSegmentedIndicatorLineView()
      indicator.indicatorWidth = 42
      indicator.indicatorColor = .designKit.accent
      segmentedView.indicators = [indicator]

      segmentedView.listContainer = listContainerView
      view.addSubview(listContainerView)

      searchView.snp.makeConstraints { make in
        make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
        make.left.right.equalToSuperview()
        make.height.equalTo(56)
      }

      segmentedView.snp.makeConstraints { make in
        make.top.equalTo(searchView.snp.bottom)
        make.left.right.equalToSuperview()
        make.height.equalTo(36)
      }

      listContainerView.snp.makeConstraints { make in
        make.top.equalTo(segmentedView.snp.bottom)
        make.left.right.bottom.equalToSuperview()
      }
    }

  @objc func tappedFees() {

    sheetHandler.sheet(chargesNotes: nil) { index in

    }
  }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension DemandNoteViewController: JXSegmentedViewDelegate {
}

extension DemandNoteViewController: JXSegmentedListContainerViewDataSource {

  func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
    return segmentedDataSource.titles.count
  }

  func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {

    let vc = DemandListViewController()
    switch index {
    case 0:
      vc.type = .unpaidFee
    default:
      vc.type = .paidFee
    }
    return vc
  }
}
